var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var communityUrl = require("url/community");
var communityUtil = require("helper/community");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var helperOpen = require("helper/open");

//UI
var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.message.indexV1;

/** 模板列表
source://view/community/message/index/messageCell4Top.ui
source://view/community/message/index/messageCell.ui
source://view/community/message/index/activityCell.ui
source://view/reuse/noCell.ui
**/

//模板代码
var template4Top=0,template4Message=1,template4Activity=2;

var noData = [{"template":3,"labelContent":"暂无内容"}];
var isNoData = true;
//当前页码
var pageNum = 1;
function loadFirstPage(isSilence) {
	isNoData = true;
	var httpHandle = http;
	if(isSilence){
		httpHandle = httpSilence;
	}
	pageNum=1;
	var params = {
		communityId: communityUtil.communityVisitId(),
		page : pageNum
	};
	httpHandle.post(httpUrl, params, function(data) {
		if(listdata.getCount()>0){
			listdata.removeAll();
		}
		//加载置顶的大喇叭
		var messageList4Top = data.messageList4Top;
		if(messageList4Top && messageList4Top.length>0){
			listdata.addData(messageList4Top);//默认第0号模板【官方或置顶的大喇叭】
			isNoData = false;
		}
		//加载-活动
		var activityList = data.activityList;//活动列表【只有一行】
		if(activityList && activityList.length>0){
			activityList[0].template =template4Activity;//指定模板【活动】
			listdata.addOne(activityList[0]);
			isNoData = false;
		}
		var messageList4Normal = data.messagePage4Normal.content;
		if(messageList4Normal && messageList4Normal.length>0){
			for(var i in messageList4Normal){
				messageList4Normal[i].template = template4Message;//指定模板【普通的大喇叭】
				listdata.addOne(messageList4Normal[i]);
			}
			isNoData = false;
		}
		if(isNoData){
			listdata.addData(noData);//没有任何信息
		}
		listview.refreshItems();
	})
}

//加载下一页
function getNextPage(){
	pageNum++;
	var params = {
		communityId: communityUtil.communityVisitId(),
		page : pageNum
	};
	http.post(httpUrl, params, function(data) {
		var messageList4Normal = data.messagePage4Normal.content;
		if(messageList4Normal && messageList4Normal.length>0){
			for(var i in messageList4Normal){
				messageList4Normal[i].template = template4Message;//指定模板【普通的大喇叭】
				listdata.addOne(messageList4Normal[i]);
			}
		}
		listview.refreshItems();
	})
}

//下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		loadFirstPage(false);
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		getNextPage();
		listview.rebound();//复位【一定要做】
	}
});

//在当前页面下订阅community_change_join的事件
do_Page.on("community_change_join", function(data) {
	loadFirstPage(false);
	listview.rebound()
});

//新增刷新
do_Page.on("community_message_tab_touch", function(data) {
	if(data.isResult==true){
		loadFirstPage(true);
	}else{
		loadFirstPage(false);
	}
});
